﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperWebSocket.SubProtocol;

namespace _21WebSocketServer.Command
{
    public class STAND : SubCommandBase<_21Session>
    {
        public override void ExecuteCommand(_21Session session, SubRequestInfo requestInfo)
        {
            var game = session.Server.Game;
            lock (game)
            {
                if (!session.Player.IsTurn)
                {
                    session.SendResponseAsync("ERROR 你不是当前行动者！");
                    return;
                }

                session.Player.IsTurn = false;
                session.SendResponseAsync("WAIT_OTHER");
                if (session.Player.IsDealer)
                {
                    game.End();
                    session.Server.Game = new Game();
                }
                else
                {
                    var nextPlayer = (from p in game.PlayerList
                                      where p.Seq == session.Player.Seq + 1
                                      select p).SingleOrDefault();
                    if (nextPlayer == null)
                        nextPlayer = game.PlayerList[0];
                    nextPlayer.Session.SendResponseAsync("WAIT");
                    nextPlayer.IsTurn = true;
                }
            }
        }
    }
}
